/**
 * Created by gongxun on 17/1/13.
 */

var GsbdxTableLayer = GameCCSBase.extend({
    mGsbdxTableCardLayer : null,
    mGsbdxTableUILayer : null,
    mKwxApplyTableLayer : null,
    mGsbdxTableRoomEndLayer : null,
    mGsbdxTableReplayLayer : null,

    //牌桌状态 0录像 1正常打牌
    mPlayerType : null,

    //统计打了多少局
    mPlayGameCount : null,

    //游戏规则
    mGameConfig : null,

    //房间号
    mGroupId : null,

    //群号
    mGroupName : null,

    //录像号
    mReplayId : null,

    //密码
    mTableEnterPwd : null,

    //是否是创建者
    mIsRoomCreator : null,
    ctor : function(){
        this._super();
        this.loadCocostudio("res/ccs/GsbdxTable.json");
        mo.gsbdxTable = this;
        this.mIsRoomCreator = false;
        mo.gsbdxCardHelper.init();

        this.loadUI();
    },

    onEnter : function(){
        this._super();
        this.runAction(cc.sequence(cc.delayTime(1), cc.callFunc(function(){
            mo.soundHelper.playMusicGameGsbdx();
        })));

        mo.eventHelper.add("GsbdxForceExitTableResponsegsbdxOK", this, function () {
            if (mo.gsbdxTable.mGameConfig.maxpan == mo.gsbdxTableStatus.mTableStatus.LeftPlayTimes){
                mo.showMsgDialogOk("退出牌局成功", function(){
                    this.quitGsbdxGame();
                }, this);
            }
        });

        mo.eventHelper.add("CancleJuRequestgsbdx", this, function(cancleJu){
            this.mKwxApplyTableLayer.updateCancelJuInfo(cancleJu);
        });
    },

    onExit : function(){
        mo.eventHelper.remove(this);
        this._super();
    },

    //正常打牌
    loadPlayCard : function(){
        this.mPlayGameCount = 0;
        this.mPlayerType = 1;
        this.mGsbdxTableUILayer.intoShowIndex(10);

        //隐藏录像界面
        this.mGsbdxTableReplayLayer.setVisible(false);

        //隐藏录像时候的ui
        this.mGsbdxTableUILayer.setIsReplay(false);
    },

    //播放录像
    loadReplay : function(rePlaygsbdx, replayId){
        this.mPlayGameCount = 0;
        this.mPlayerType = 0;
        this.mGsbdxTableUILayer.intoShowIndex(10);

        //录像规则
        mo.gsbdxTable.setGsbdxConfig(rePlaygsbdx.Result.CfgJson, "", "");

        //打开录像界面 并且播放
        this.mGsbdxTableReplayLayer.setVisible(true);
        this.mGsbdxTableReplayLayer.setReplayId(replayId);
        this.mGsbdxTableReplayLayer.onStart();

        //隐藏录像时候的ui
        this.mGsbdxTableUILayer.setIsReplay(true);

        this.mReplayId = replayId;
        mo.gsbdxTableStatus.setReplayMsg(rePlaygsbdx);
        mo.gsbdxTableStatus.playReplay();
    },

    //显示代开房人
    setShowRoomCreate : function(user){
        if (user != null){
            var kwxDaikaifangWidget = new KwxDaikaifangWidget();
            kwxDaikaifangWidget.setUserInfo(user);
            this.addChild(kwxDaikaifangWidget);
            kwxDaikaifangWidget.setPosition(cc.p(mo.gameSize.x - 250, mo.gameSize.y - 10));
        }
    },

    loadUI : function(){
        //sprite
        cc.spriteFrameCache.addSpriteFrames("res/ttzCards.plist", "res/ttzCards.png");
        cc.spriteFrameCache.addSpriteFrames("res/emojis.plist", "res/emojis.png");
        cc.spriteFrameCache.addSpriteFrames("res/emojis_index.plist", "res/emojis_index.png");

        //牌层
        this.mGsbdxTableCardLayer = new GsbdxTableCardLayer();
        this.addChild(this.mGsbdxTableCardLayer);

        //ui层
        this.mGsbdxTableUILayer = new GsbdxTableUILayer();
        this.addChild(this.mGsbdxTableUILayer);

        //取消
        this.mKwxApplyTableLayer = new KwxApplyTableLayer("gsbdx");
        this.addChild(this.mKwxApplyTableLayer);

        //录像层
        this.mGsbdxTableReplayLayer = new GsbdxTableReplayLayer();
        this.addChild(this.mGsbdxTableReplayLayer);
    },

    setGsbdxConfig : function(Cfgjson, tablePassWord, GroupName){
        this.mGroupName = GroupName;
        this.mTableEnterPwd = tablePassWord;
        this.mGameConfig = JSON.parse(Cfgjson);
        //密码
        if (mo.gameHelper.developerVersion == "3.0"){

            mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_textRoomId"), "密码：");
            mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_roomId"), "" + tablePassWord);
        }

        //info
        var infoStr = "";
        var json = this.mGameConfig;
        var wanfa = ["大众玩法", "", "河南玩法"];
        infoStr += (wanfa[json.ruleid - 1] + "    ");
        infoStr += (json.maxpan + "局" + "    ");
        var hosttypeStr = ["轮庄", "连庄", "霸王庄"];
        infoStr += (hosttypeStr[json.hosttype] + "    ");
        if (json.addratetime == 0){
            infoStr += "每次选分";
        }else{
            infoStr += "固定" + json.basepoint + "分";
        }
        if (json.lod != 0){
            infoStr += "    生死战:" + json.lod;
        }
        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_tableRule"), "" + infoStr);
    },

    quitGsbdxGame : function(){
        if (mo.gameHelper.developerVersion == "2.0"){
            mo.sceneHelper.runSceneHall();
        }else if (mo.gameHelper.developerVersion == "3.0"){
            mo.sceneHelper.exitGameLayer();
            if (this.mGroupId != null){
                mo.hallLayer.intoGroup(this.mGroupId);
            }
        }
    },

    //显示牌墙
    showCardWell : function(cardNums, isAnim){
        this.mGsbdxTableCardLayer.showCardWell(cardNums, isAnim);
    },

    //发牌 toViewSeat发第一次牌的玩家位置 isAnim的时候 给牌赋值 给牌型赋值
    setFapaiAnim : function(dices, toViewSeat, cardNumss, cardTypes, isAnim){
        if (this.mPlayerType == 0){
            isAnim = false;
        }

        //发牌
        this.mGsbdxTableCardLayer.setFapaiAnim(dices, toViewSeat, isAnim);

        //重置cardtype
        this.mGsbdxTableUILayer.setResetKaipai();

        if (isAnim){
            this.mGsbdxTableUILayer.setPlayMaidinglishou();
        }else{
            //如果没有动画就给玩家牌赋值
            for(var i = 0; i < 5; i++){
                if (cardNumss[i] != null){
                    //牌
                    this.mGsbdxTableCardLayer.setKaipaiSeat(i, cardNumss[i], false);
                    //ui动画
                    this.mGsbdxTableUILayer.setKaipaiSeat(i, cardTypes[i], false);
                }
            }
        }

        //开牌按钮
        this.mGsbdxTableUILayer.showKaipaiBtn(isAnim);
    },

    //开牌
    setKaipaiSeat : function(seat, cardNums, cardType){
        //牌
        this.mGsbdxTableCardLayer.setKaipaiSeat(seat, cardNums, true);
        //ui动画
        this.mGsbdxTableUILayer.setKaipaiSeat(seat, cardType, true);
        //播放牌型音效
        var sex = mo.gsbdxTableStatus.getSexForViewSeat(seat);
        mo.soundHelper.playGsbdxCardType(sex, cardNums);
    },

    //大结算
    onShowJuEndedLayer : function(juEndedEventgsbdx){
        //判断是否需要直接退出 如果剩余局数和设置里面的局数一致 就直接退出
        var ruleJson = JSON.parse(juEndedEventgsbdx.CfgJson);

        if (mo.gsbdxTableStatus.mTableStatus.LeftPlayTimes == ruleJson.maxpan){
            mo.showMsgDialogOk("牌局已经解散", function(){
                this.quitGsbdxGame();
            }, this);
        }else{


            this.mGsbdxTableRoomEndLayer = new GsbdxTableRoomEndLayer();
            mo.sceneHelper.showEndInfoLayer(this.mGsbdxTableRoomEndLayer);
            // this.addChild(this.mGsbdxTableRoomEndLayer);
            this.mGsbdxTableRoomEndLayer.setRoomEndInfo(juEndedEventgsbdx);

            if (juEndedEventgsbdx.EndFlag != 1){
                //自然结束 通过当局结算打开
                this.mGsbdxTableRoomEndLayer.setVisible(false);
            }else{
                this.mGsbdxTableRoomEndLayer.setVisible(true);
            }
        }
    },

    //更新剩余牌局
    updateLastPlayTimes : function(){
        var time = mo.gsbdxTableStatus.mTableStatus.LeftPlayTimes;
        if (time != null){
            if (time < 0){
                time = 0;
            }
            mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_leftJu"), time + "");
            if (mo.gameHelper.developerVersion == "2.0"){
                mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_roomId"), "" + mo.gsbdxTableStatus.mTableStatus.TableID);
            }
        }
    },

    //邀请玩家
    onInvite : function(){
        var gameName = "gsbdx";
        var configSummary = mo.gameHelper.getRuleConfigSummary(this.mGameConfig, gameName);
        configSummary += "，就等你来了(点击加入)";
        var tableId = mo.gsbdxTableStatus.mTableStatus.TableID;

        var title = "桌号:[" + tableId + "]" + "-" + mo.gameHelper.projectName + "-推筒子";
        var description = configSummary;
        mo.gameHelper.onWxShareWeb(title, description, mo.gameUserInfo.getExternalUrl(1, tableId));
    },

    //分享录像
    onShareReplay : function(){
        var gameName = "gsbdx";
        var configSummary = mo.gameHelper.getRuleConfigSummary(this.mGameConfig, gameName);
        configSummary += "，在游戏中输入录像id就能看到我的精彩回放了";

        var title = "录像id:" + this.mReplayId.substr(7) + "-" + mo.gameHelper.projectName + "-推筒子";
        var description = configSummary;

        mo.gameHelper.onWxShareWeb(title, description, mo.gameUserInfo.getExternalUrl(2, this.mReplayId.substr(7)));
    },

    //牌桌闪动
    onPlayTableOpAnim : function(){
        this.runAction(cc.sequence(
            cc.delayTime(0.50),
            cc.moveTo(0.05, cc.p(0, -15)),
            cc.moveTo(0.05, cc.p(0, 0)),
            cc.moveTo(0.03, cc.p(0, -10)),
            cc.moveTo(0.03, cc.p(0, 0))
        ));
    }
});